///////////////////////////////////////////////////////////
//  ControlCommand.cpp
//  Implementation of the Class ControlCommand
//  Created on:      21-Sep-2013 12:06:49 PM
//  Original author: Jakkaphan Aiemcharoenjit
///////////////////////////////////////////////////////////

#include "ControlCommand.h"


using namespace UI;
using namespace UI::CMD;

ControlCommand* UI::CMD::ControlCommand::g_cotrolCmdInstance = NULL;

ControlCommand::ControlCommand()
{

}


ControlCommand::~ControlCommand()
{
  std::map<ICommand::CommandType, ICommand*>::iterator itCommand;
  itCommand = this->m_allCommands.begin();
  while(itCommand != this->m_allCommands.end())
  {
    delete itCommand->second;
    itCommand++;
  }

  this->m_allCommands.clear();
}


ControlCommand* ControlCommand::GetInstance()
{
  if(!g_cotrolCmdInstance)
    g_cotrolCmdInstance = new ControlCommand();

	return  g_cotrolCmdInstance;
}


void ControlCommand::Release()
{
  if(g_cotrolCmdInstance)
    delete g_cotrolCmdInstance;

  g_cotrolCmdInstance = NULL;
}


void ControlCommand::SetCommand(ICommand* command)
{
  this->m_allCommands.insert(std::make_pair(command->GetType(), command));
}


void ControlCommand::OnNew(ControlCommand::ControlView ctrlView)
{
  switch(ctrlView)
  {
  case TEMPLATE_CTRL:
    this->m_allCommands[ICommand::NEWTEMPLATE_CMD]->Execute();
    break;
  case CHECKLIST_CTRL:
    this->m_allCommands[ICommand::NEWCHECKLIST_CMD]->Execute();
    break;
  case ITEM_CTRL:
    this->m_allCommands[ICommand::NEWITEM_CMD]->Execute();
    break;
  case CHOICE_CTRL:
    this->m_allCommands[ICommand::NEWCHOICE_CMD]->Execute();
    break;
  default: break;
  }
}


void ControlCommand::OnInsert()
{
  this->m_allCommands[ICommand::INSERTITEM_CMD]->Execute();
}


void ControlCommand::OnEdit(ControlView ctrlView)
{
  switch(ctrlView)
  {
  case TEMPLATE_CTRL:
    this->m_allCommands[ICommand::EDITTEMPLATE_CMD]->Execute();
    break;
  case CHECKLIST_CTRL:
    this->m_allCommands[ICommand::EDITCHECKLIST_CMD]->Execute();
    break;
  case ITEM_CTRL:
    this->m_allCommands[ICommand::EDITITEM_CMD]->Execute();
    break;
  case CHOICE_CTRL:
    this->m_allCommands[ICommand::EDITCHOICE_CMD]->Execute();
    break;
  default: break;
  }
}


void ControlCommand::OnSave(ControlView ctrlView)
{
  switch(ctrlView)
  {
  case TEMPLATE_CTRL:
    this->m_allCommands[ICommand::SAVETEMPLATE_CMD]->Execute();
    break;
  case CHECKLIST_CTRL:
    this->m_allCommands[ICommand::SAVECHECKLIST_CMD]->Execute();
    break;
  case ITEM_CTRL:
    this->m_allCommands[ICommand::SAVEITEM_CMD]->Execute();
    break;
  case CHOICE_CTRL:
    this->m_allCommands[ICommand::SAVECHOICE_CMD]->Execute();
    break;
  default: break;
  }
}


void ControlCommand::OnBack(ControlView ctrlView)
{
  switch(ctrlView)
  {
  case TEMPLATE_CTRL:
    this->m_allCommands[ICommand::BACKTEMPLATE_CMD]->Execute();
    break;
  case CHECKLIST_CTRL:
    this->m_allCommands[ICommand::BACKCHECKLIST_CMD]->Execute();
    break;
  default: break;
  }
}


void ControlCommand::OnViewDetail(ControlView ctrlView)
{
  switch(ctrlView)
  {
  case TEMPLATE_CTRL:
    this->m_allCommands[ICommand::VIEWTEMPLATE_CMD]->Execute();
    break;
  case CHECKLIST_CTRL:
    this->m_allCommands[ICommand::VIEWCHECKLIST_CMD]->Execute();
    break;
  default: break;
  }
}


void ControlCommand::OnSendMail()
{
  this->m_allCommands[ICommand::SENDMAIL_CMD]->Execute();
}